﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace EnterpriseFund.Controllers
{
        [OutputCache(Duration = 0, VaryByParam = "none")]
    public class AllowanceFileAuditController : AdminBaseController
    {
        //
        // GET: /AllowanceFileAudit/
        [AuthorityActionFilter("m_AllowanceFileAudit", Order = 1)]
        [LogPageActionFilter("出具文件", Order = 2)]
        public ActionResult Index()
        {
            string strwhere = " FStatus ='1' and FFileNo is null";
            List<Model.AllowanceFile> AllowanceFileList = new List<Model.AllowanceFile>();
            AllowanceFileList = GetAllowanceFileListList(strwhere, 1);
            return View(AllowanceFileList);
        }

        public ActionResult AllowanceFileAuditPrint(string FAllowanceFileID)
        {
            ViewData["FAllowanceFileID"] = FAllowanceFileID;
            return View();
        }
           
        public ActionResult AllowanceFileAuditPrintPartial()
        {
            string FAllowanceFileID = Request["FAllowanceFileID"];
            Model.AllowanceFile model = new Model.AllowanceFile();
            BLL.AllowanceFile allowanceFileBLL = new BLL.AllowanceFile();
            model = allowanceFileBLL.GetModel(FAllowanceFileID);
            BLL.AllowanceFileEntry allowanceFileEntryBLL = new BLL.AllowanceFileEntry();
            DataTable dt = allowanceFileEntryBLL.GetList("FAllowanceFileID='" + model.FAllowanceFileID + "'").Tables[0];
            dt = GetLookData(dt);
            DataTable dt2 = new DataTable();
            dt2.Columns.Add("xh");
            dt2.Columns.Add("Title");
            dt2.Columns.Add("FNote");
            dt2.Columns.Add("FAmount");
            dt2.Columns.Add("NSRMC");
            int i = 1;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["NSRMC"].ToString() != "合计")
                {
                    DataRow nr = dt2.NewRow();
                    nr["xh"] = i;
                    nr["Title"] = model.FFileName;
                    nr["FNote"] = dr["FNote"];
                    nr["FAmount"] = dr["FAmount"];
                    nr["NSRMC"] = dr["NSRMC"];
                    dt2.Rows.Add(nr);
                    i++;
                }
            }
            DataSet ds = new DataSet();
            dt2.TableName = "dt";
            ds.Tables.Add(dt2);
            ViewData["data"] = ds;
            return View();
        }

        public string VerifyFile(string fAllowanceFileID)
        {
            BLL.AllowanceFile allowanceFileBLL = new BLL.AllowanceFile();
            if (allowanceFileBLL.VerifyFile(fAllowanceFileID))
            {
                return "true";
            }
            else
            {
                return "false";
            }
        }

        public ActionResult AllowanceFileAuditListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = "1=1 ";
            string FStatus = Request.Params["FStatus"];
            string NSRBM = Request.Params["NSRBMQ"];
            if (!string.IsNullOrEmpty(FStatus))
            {
                switch (FStatus)
                {
                    case "1":
                        strwhere += "  and ( FStatus ='1' and FFileNo is null )";
                        break;
                    case "2":

                        strwhere += "  and   FStatus='2' and FFileNo is not null ";
                        break;
                }
            }
            else
            {
                strwhere += "  and ( FStatus ='1' and FFileNo is null)";
            }

            if (!string.IsNullOrEmpty(NSRBM))
            {
                strwhere += " and  (FFileName like '%" + NSRBM + "%' or FFileNo like '%" + NSRBM + "%' )";
            }
           // string strwhere = " FStatus ='1' and FFileNo is null";
            List<Model.AllowanceFile> AllowanceFileList = new List<Model.AllowanceFile>();
            AllowanceFileList = GetAllowanceFileListList(strwhere, pageIndex);
            return PartialView("AllowanceFileAuditListPartial", AllowanceFileList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.AllowanceFile> GetAllowanceFileListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.AllowanceFile AllowanceFileBLL = new BLL.AllowanceFile();
            string fiedls = "FAllowanceFileID";
            int total;
            DataSet ds = commonBll.GetListByPage("fund.v_AllowanceFile", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FAllowanceFileID", out total);
            List<Model.AllowanceFile> AllowanceFileList = AllowanceFileBLL.DataTableToList(ds.Tables[0]);
            foreach (Model.AllowanceFile a in AllowanceFileList)
            {
                a.sumFAmount = a.sumFAmount / 10000;
            }
            List<Model.AllowanceFile> AllowanceFileAllList = new List<Model.AllowanceFile>();
            total = AllowanceFileBLL.GetRecordCount(strWhere);
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                AllowanceFileAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (AllowanceFileList.Count > i)
                {
                    AllowanceFileAllList[((pageIndex - 1) * pageRowSize) + i] = AllowanceFileList[i];
                }
            }

            return AllowanceFileAllList;
        }

        public ActionResult AllowanceFileAuditEdit(string FAllowanceFileID)
        {
            BLL.AllowanceFile allowanceFileBLL = new BLL.AllowanceFile();
            Model.AllowanceFile model = allowanceFileBLL.GetModel(FAllowanceFileID);
            ViewData["FFileName"] = model.FFileName;
            if (model.FStatus == "1")
            {
                model.FNote = model.FFileName;
            }
            BLL.AllowanceFileEntry allowanceFileEntryBLL = new BLL.AllowanceFileEntry();
            DataTable dt = allowanceFileEntryBLL.GetList("FAllowanceFileID='" + model.FAllowanceFileID + "'").Tables[0];
            ViewData["showdata"] = GetLookData(dt);
            ViewData["FAllowanceFileID"] = FAllowanceFileID;
            return View("AllowanceFileAuditEdit",model);
        }

        public ActionResult Save()
        {
            try
            {
                Model.AllowanceFile model = new Model.AllowanceFile();
                model.FFileNo = Fetch.Post("FFileNo");
                model.FFileTransDate = DateTime.Now;
                model.FNote = Fetch.Post("FNote");
                model.FAllowanceFileID = Fetch.Post("hidFAllowanceFileID");
                BLL.AllowanceFile allowanceFileBLL = new BLL.AllowanceFile();
                allowanceFileBLL.Update(model);
                WorkFlow("t_AllowanceFile", "20", model.FAllowanceFileID, "open", "");
                ViewData["jsAlert"] = "操作成功";
            }
            catch(Exception ex)
            {
                ViewData["jsAlert"] = ex.Message;
            }

            return AllowanceFileAuditEdit(Fetch.Post("hidFAllowanceFileID"));
        }



        public DataTable GetLookData(DataTable dt)
        {
            foreach (DataRow drz in dt.Rows)
            {
                drz["FAmount"] = decimal.Parse(drz["FAmount"].ToString()) / 10000;
            }
            dt.Columns["FAmount"].DataType = typeof(decimal);
            decimal sumse = dt.AsEnumerable().Sum(p => p.Field<decimal>("FAmount"));
            DataRow dr = dt.NewRow();
            dr["NSRMC"] = "合计";
            dr["FAmount"] = sumse;
            dt.Rows.Add(dr);
            return dt;

        }
    }
}
